#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>

#include "GeomSvc.h"
#include "TriggerRoad.h"

using namespace std;

int main(int argc, char **argv)
{
  cout << "Converting " << argv[1] << " to ROOT file: " << argv[2] << endl;

  GeomSvc *p_geomSvc = GeomSvc::instance();
  p_geomSvc->init("geometry_R962");

  Double_t mass, xF, x1, x2;
  Double_t weight;
  Double_t x0, y0, z0;
  
  Double_t px1, py1, pz1, p1;
  Double_t px2, py2, pz2, p2;

  Int_t nHodoHits1;
  Int_t detectorID1[100];
  Int_t elementID1[100];
  Double_t xp[100], yp[100];

  Int_t nHodoHits2;
  Int_t detectorID2[100];
  Int_t elementID2[100];
  Double_t xm[100], ym[100];

  Int_t nHits;
  Int_t detectorID[500];
  Int_t elementID[500];
  Double_t x[100], y[100];

  TFile *saveFile = new TFile(argv[2], "recreate");
  TTree *saveTree = new TTree("save", "save");

  saveTree->Branch("weight", &weight, "weight/D");
  saveTree->Branch("mass", &mass, "mass/D");
  saveTree->Branch("xF", &xF, "xF/D");
  saveTree->Branch("x1", &x1, "x1/D");
  saveTree->Branch("x2", &x2, "x2/D");
  saveTree->Branch("x0", &x0, "x0/D");
  saveTree->Branch("y0", &y0, "y0/D");
  saveTree->Branch("z0", &z0, "z0/D");
  
  saveTree->Branch("nHits", &nHits, "nHits/I");
  saveTree->Branch("detectorID", detectorID, "detectorID[nHits]/I");
  saveTree->Branch("elementID", elementID, "elementID[nHits]/I");
  saveTree->Branch("x", x, "x[nHits]/D");
  saveTree->Branch("y", y, "y[nHits]/D");

  saveTree->Branch("px1", &px1, "px1/D");
  saveTree->Branch("py1", &py1, "py1/D");
  saveTree->Branch("pz1", &pz1, "pz1/D");
  saveTree->Branch("p1", &p1, "p1/D");

  saveTree->Branch("nHodoHits1", &nHodoHits1, "nHodoHits1/I");
  saveTree->Branch("detectorID1", &detectorID1, "detectorID1[nHodoHits1]/I");
  saveTree->Branch("elementID1", &elementID1, "elementID1[nHodoHits1]/I");
  saveTree->Branch("xp", &xp, "xp[nHodoHits1]/D");
  saveTree->Branch("yp", &yp, "yp[nHodoHits1]/D");

  saveTree->Branch("px2", &px2, "px2/D");
  saveTree->Branch("py2", &py2, "py2/D");
  saveTree->Branch("pz2", &pz2, "pz2/D");
  saveTree->Branch("p2", &p2, "p2/D");

  saveTree->Branch("nHodoHits2", &nHodoHits2, "nHodoHits2/I");
  saveTree->Branch("detectorID2", &detectorID2, "detectorID2[nHodoHits2]/I");
  saveTree->Branch("elementID2", &elementID2, "elementID2[nHodoHits2]/I");
  saveTree->Branch("xm", &xm, "xm[nHodoHits2]/D");
  saveTree->Branch("ym", &ym, "ym[nHodoHits2]/D");

  Int_t hodoIDs[2][4] = {{25, 31, 33, 39}, {26, 32, 34, 40}};

  fstream fin(argv[1], ios::in);
  char buffer[500];
  while(fin.getline(buffer, 500))
    {
      istringstream stringBuf(buffer);

      nHodoHits1 = 4;
      nHodoHits2 = 4;

      Int_t dimuonID;
      string dID1[4], dID2[4];
      Double_t tempPt;

      stringBuf >> dimuonID >> elementID2[0] >> dID2[0] >> elementID2[1] >> dID2[1]
	>> elementID2[2] >> dID2[2] >> elementID2[3] >> dID2[3] >> px2 >> py2 >> pz2
	>> elementID1[0] >> dID1[0] >> elementID1[1] >> dID1[1] >> elementID1[2] >> dID1[2]
	>> elementID1[3] >> dID1[3] >> px1 >> py1 >> pz1 >> mass >> xF >> x1 >> x2
	>> tempPt >> z0 >> weight;

      nHits = 0;
      for(int i = 0; i < 4; i++)
	{
	  detectorID1[i] = dID1[i] == 'B' ? hodoIDs[0][i] : hodoIDs[1][i];
	  detectorID2[i] = dID2[i] == 'B' ? hodoIDs[0][i] : hodoIDs[1][i];

	  detectorID[nHits] = detectorID1[i];
	  elementID[nHits] = elementID1[i];
	  ++nHits;

	  detectorID[nHits] = detectorID2[i];
	  elementID[nHits] = elementID2[i];
	  ++nHits;
	} 

      saveTree->Fill();
    }

  saveFile->cd();
  saveTree->Write();
  saveFile->Close();
  
  return 1;
}
